// Copyright 2009 Google Inc. All Rights Reserved

//goog.provide('cz');
//goog.provide('cz.mzk');
//goog.provide('cz.mzk.kramerius');
//goog.provide('cz.mzk.kramerius.audio');
//goog.provide('cz.mzk.kramerius.audio.player');
goog.provide('cz.mzk.kramerius.audio.player.Tracks');

goog.require('goog.dom');
//goog.require('goog.dom.DomHelper');
goog.require('goog.ui.Zippy');
goog.require('goog.ui.Button');
goog.require('goog.ui.Control');




/**
 * Iterates over a list of note data objects, creates a Track instance
 * for each one, and tells the instance to build its DOM structure.
 * @param {Array.<Object>} data The tracks data.
 * @param {Element} trackContainer The element under which DOM nodes for
 *     the tracks should be added.
 * @return {Array.<cz.mzk.kramerius.audio.player.Track>} An array containing the resulting
 *     instances.
 */
cz.mzk.kramerius.audio.player.makeTracks = function(data, trackContainer) {
    var tracks = [];
    for (var i = 0; i < data.length; i++) {
        var track = 
        new cz.mzk.kramerius.audio.player.Track(data[i].title, data[i].pid, trackContainer);
        tracks.push(track);
        track.makeTrackDom();
    }
    return tracks;
};



/**
 * Manages the data and interface for a single track.
 * @param {string} title The title of the note.
 * @param {string} pid The pid of track
 * @param {Element} trackContainer The element under which DOM nodes for
 *     the tracks should be added.
 * @constructor
 */
cz.mzk.kramerius.audio.player.Track = function(title, pid, trackContainer) {
    //    console.log(title);
    //    console.log(pid)
    this.title = title;
    this.pid = pid;
    this.parent = trackContainer;
};


cz.mzk.kramerius.audio.player.Track.prototype.urlPrefix = function(){
    var servletUrl = 'http://localhost:8084/K4AudioPOC/AudioServlet/';
    return servletUrl + this.pid + '/';
}

cz.mzk.kramerius.audio.player.Track.prototype.sourceElementMp3 = function(){
    //    var sourceElement = goog.dom.createElement('source');
    //    var url = this.urlPrefix() + "MP3";
    //    var audioElement=  goog.dom.createDom('audio', {
    //        'type':'audio/mpeg',
    //        'src':url
    //    });
    //    goog.dom.appendChild(sourceElement,audioElement);
    //    return sourceElement;
    }

cz.mzk.kramerius.audio.player.Track.prototype.sourceElement = function(datastreamId, mimeType){
    var url = this.urlPrefix() + datastreamId;
    return  goog.dom.createDom('source', {
        'type':mimeType,
        'src':url
    });
    return sourceElement;
}

/**
 * Creates the DOM structure for the note and adds it to the document.
 */

cz.mzk.kramerius.audio.player.Track.prototype.makeTrackDom = function() {
    // Create DOM structure to represent the track.
    //    this.headerElement = goog.dom.createDom('div',
    //    {
    //        'style': 'background-color:#EEE'
    //    }, this.title);
    this.contentElement = goog.dom.createDom('audio',{
        'controls':'controls', 
        //'autoplay':'x'
        'preload':'metadata'
    }, null);    
    //sources
    goog.dom.appendChild(this.contentElement, this.sourceElement('MP3','audio/mpeg'));
    goog.dom.appendChild(this.contentElement, this.sourceElement('OGG','audio/ogg'));
    var fallbackText = goog.dom.createTextNode('Your browser does not support the <code>audio</code> element.');
    goog.dom.appendChild(this.contentElement,fallbackText);
    
    
    
    //goog.dom.DomHelper.appendChild(this.contentElement, srcElement);
    //this.contentElement = goog.dom.createDom('div', null, this.pid);
  
  
    var newTrack = goog.dom.createDom('div', null,
        this.headerElement, this.contentElement);

    // Add the note's DOM structure to the document.
    goog.dom.appendChild(this.parent, newTrack);
    
//return new goog.ui.Zippy(this.headerElement, this.contentElement);
};


//cz.mzk.kramerius.audio.player.alert = function(){
//    window.alert("test");
//}


cz.mzk.kramerius.audio.player.alert = function(){
    var button = goog.ui.Button("initialized");
}


//cz.mzk.kramerius.audio.player.appendHandler = function (rotateButton){
//    goog.events.listen(rotateButton, goog.events.EventType.CLICK, cz.mzk.kramerius.audio.player.resetHiddenButton);
//}
//
//cz.mzk.kramerius.audio.player.resetHiddenButton = function(e){
//    e.setProperty("hidden", "hidden", 1);
//}

cz.mzk.kramerius.audio.player.makeButtons = function(container){
    var rotateButton = new goog.ui.Button('rotate');
    //console.log('container: ' + container);
    //console.log(rotateButton);
    console.log(rotateButton.getContent());
    
    var testButton = new goog.ui.Button("test");
    goog.dom.appendChild(container, rotateButton);
    goog.dom.appendChild(container, testButton);
}

cz.mzk.kramerius.audio.player.makeTracks = function(data, trackContainer) {
    var tracks = [];
    for (var i = 0; i < data.length; i++) {
        var track = 
        new cz.mzk.kramerius.audio.player.Track(data[i].title, data[i].pid, trackContainer);
        tracks.push(track);
        track.makeTrackDom();
    }
    return tracks;
};